<template>
  <div class="c-Home">
    <h3>其它子应用</h3>
    <div>token：{{ token }}</div>
    <div>
      <div @click="rsaEncrypt">rsa加密</div>
      <div @click="rsaDecrypt">rsa解密</div>
    </div>
    <div>
      <div @click="aesEncrypt">aes加密</div>
      <div @click="aesDecrypt">aes解密</div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex"
import { JSEncrypt } from 'jsencrypt'
import CryptoJS from "crypto-js";
import { pubKey, priKey, text, desc } from './mock'
const jsEncrypt = new JSEncrypt()  // 创建加密对象实例
const textString = JSON.stringify(text)

// aes密钥（8位、16位、32位）
const key = CryptoJS.enc.Utf8.parse("d3cara43lkiypoyt"); // 秘钥 16*n
const iv = CryptoJS.enc.Utf8.parse("d3cara43lkiypoyt"); // 偏移量

export default {
  name: "Home",
  data() {
    return {
      rsaPassWord: '',
      aesPassWord: ''
    };
  },
  computed: {
    ...mapGetters(["token"])
  },
  created() {
  },
  methods: {
    // rsa加密
    rsaEncrypt() {
      //之前ssl生成的公钥，复制的时候要小心不要有空格
      console.time('rsaEncrypt')
      jsEncrypt.setPublicKey(pubKey)//设置公钥
      this.rsaPassWord = jsEncrypt.encrypt(textString)  // 对内容进行加密
      console.timeEnd('rsaEncrypt')
      console.log(this.rsaPassWord)
    },
    // rsa解密
    rsaDecrypt() {
      //之前ssl生成的秘钥
      console.time('rsaDecrypt')
      jsEncrypt.setPrivateKey(priKey)//设置秘钥
      let result = jsEncrypt.decrypt(this.rsaPassWord)//解密之前拿公钥加密的内容
      console.timeEnd('rsaDecrypt')
      console.log(result)
    },
    // aes加密
    aesEncrypt() {
      console.time('aesEncrypt')
      const srcs = CryptoJS.enc.Utf8.parse('textString');
      let encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      })
      this.aesPassWord = encrypted.toString()
      console.timeEnd('aesEncrypt')
      console.log(typeof this.aesPassWord)
      console.log(this.aesPassWord)
    },
    // aes解密
    aesDecrypt() {
      console.time('aesDecrypt')
      let decrypted = CryptoJS.AES.decrypt(desc, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      })
      let result = decrypted.toString(CryptoJS.enc.Utf8)
      console.timeEnd('aesDecrypt')
      console.log(result)
    },
  }
};
</script>

<style lang="less" scoped>
.c-Home {
  .c-LinkList {
    padding: 0;
    margin: 0;
    li {
      list-style: none;
      cursor: pointer;
      line-height: 0.36rem;
      a {
        font-size: 0.24rem;
        color: #368fff;
      }
    }
  }
  .link-rect {
    font-size: 0.24rem;
    color: #368fff;
    cursor: pointer;
    line-height: 0.36rem;
  }
  .img-box {
    width: 4.8rem;
    height: 4.8rem;
    background: url("../../../../assets/images/default/course-null.png") center
      no-repeat;
    background-size: 100%;
  }
}
</style>

